package com.y.lintcode;

import java.util.Arrays;

/**
 * @author Y2014188432
 * 两个有序数组合并成新的有序数组
 */
public class Test4 {
    public static void main(String[] args) {
        int[] a = {1,3,5,7,9};
        int[] b = {1,5,6,7,9};
        int[] c = test(a,b);
        System.out.println(Arrays.toString(c));
    }

    public static int[] test(int[] a,int[] b){
        int[] result = new int[a.length+b.length];
        //实现
        int res = 0;
        int ai = 0;
        int bi = 0;
        while (true){
            if (ai == a.length){
                while (bi != b.length){
                    result[res] = b[bi];
                    res ++;
                    bi ++;
                }
                break;
            }
            if (bi == b.length){
                while (ai != a.length){
                    result[res] = a[ai];
                    res ++;
                    ai ++;
                }
                break;
            }

            if (a[ai] < b[bi]){
                result[res] = a[ai];
                res ++;
                ai ++;
            } else {
                result[res] = b[bi];
                res ++;
                bi ++;
            }
        }
        return  result;
    }
}
